package edu.pku.sei.transformation.editor.views.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import edu.pku.sei.transformation.editor.model.goal.Description;

public class GoalDescriptionDialog {
	public static int CANCEL = 1;
	public static int OK = 0;
	public static int DELETE = -1;
	private boolean isDeleteable = true;
	
	private int returnCode = SWT.CANCEL;
	private Description description;
	private StyledText descriptionText;
	private Text summaryText;
	protected Shell shell;
	private String title = "content";
	
	private boolean isDescriptionChangeable = true;

	private boolean isSummaryChangeable = true;
	
	public boolean isDescriptionChangeable() {
		return isDescriptionChangeable;
	}
	public void setDescriptionChangeable(boolean isDescriptionChangeable) {
		this.isDescriptionChangeable = isDescriptionChangeable;
	}
	public boolean isSummaryChangeable() {
		return isSummaryChangeable;
	}
	public void setSummaryChangeable(boolean isSummaryChangeable) {
		this.isSummaryChangeable = isSummaryChangeable;
	}
	public static void main(String[] args) {
		GoalDescriptionDialog d = new GoalDescriptionDialog();
		d.open();
	}
	public GoalDescriptionDialog() {
		shell = new Shell(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getWorkbenchWindow().getShell(),
				SWT.DIALOG_TRIM | SWT.RESIZE | SWT.SYSTEM_MODAL);
//		shell = new Shell();
	}

	public int open() {
		final Display display = Display.getDefault();
		createDialogArea(shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		return returnCode;
	}

	protected Control createDialogArea(Composite parent) {
		shell.setText(title);
		shell.setSize(300, 500);

		GridLayout shellGL = new GridLayout();
		shellGL.numColumns = 1;
		shell.setLayout(shellGL);
		
		Label sumTitle = new Label(shell, SWT.NONE);
		sumTitle.setText("Summary:");
		
		summaryText = new Text(shell, SWT.SINGLE | SWT.BORDER);
		summaryText.setText(description.getSummary());
		if (!isSummaryChangeable)
			summaryText.setEditable(false);
		
		GridData t = new GridData();
		t.horizontalAlignment = SWT.FILL;
		t.grabExcessHorizontalSpace = true;
		summaryText.setLayoutData(t);
		
		
		Label desTitle = new Label(shell, SWT.NONE);
		desTitle.setText("Description:");
		
//		StyledText 
		descriptionText = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		descriptionText.setText(description.getContent());
		descriptionText.setWordWrap(true);
		if (!isDescriptionChangeable)
			descriptionText.setEditable(false);

		GridData d = new GridData();
		d.grabExcessHorizontalSpace = true;
		d.grabExcessVerticalSpace = true;
		d.horizontalAlignment = SWT.FILL;
		d.verticalAlignment = SWT.FILL;
		descriptionText.setLayoutData(d);
		

		Composite buttonComp = new Composite(shell, SWT.NONE);
		GridData buttonCompGD = new GridData();
		buttonCompGD.horizontalAlignment = GridData.CENTER;
		buttonComp.setLayoutData(buttonCompGD);

		RowLayout buttonCompRL = new RowLayout();
		buttonCompRL.pack = false;
		buttonCompRL.spacing = 5;
		buttonComp.setLayout(buttonCompRL);
		
		
		// OK button
		Button okButton = new Button(buttonComp, SWT.NONE);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (isDescriptionChangeable)
					description.setContent(descriptionText.getText());
				if (isSummaryChangeable)
					description.setSummary(summaryText.getText());
				returnCode = OK;
				shell.dispose();
			}
		});
		okButton.setText("OK");
		//cancel button
		Button cancelButton = new Button(buttonComp, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				returnCode = CANCEL;
				shell.dispose();
			}
		});
		cancelButton.setText("Cancel");
		//delete button
		Button deleteButton = new Button(buttonComp, SWT.NONE);
		deleteButton.setEnabled(isDeleteable);
		deleteButton.setText("Delete");
		deleteButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				returnCode = DELETE;
				shell.dispose();
			}
		});

		return shell;
	}

	public Description getDescription() {
		return description;
	}

	public void setDescription(Description description) {
		this.description = description;
	}
	
	public void setTitle(String s) {
		this.title = s;
	}
	
	public void setDeleteable(boolean isDeleteable) {
		this.isDeleteable = isDeleteable;
	}
}
